package orderServiceImpl

import (
	model "awesomeProject/src/main/Models"
	"awesomeProject/src/main/db"
)

//todo 这里也不太懂
type OrderServiceImpl struct {
	orderDao db.IDao
}

//todo 没看懂语法
func NewOrderServiceImpl(orderDao db.IDao) *OrderServiceImpl {
	//return &OrderServiceImpl{orderDao: orderDao}
	return &OrderServiceImpl{orderDao: orderDao}
}

func (osi *OrderServiceImpl) GetAllOrder() (b []*model.DemoOrder, err error) {
	//allOrder, err := osi.GetAllOrder()
	orderList, err := osi.orderDao.GetAllOrder()
	if err !=nil {
		return nil,err
	}
	return orderList,nil
}



func (osi *OrderServiceImpl) AddNewOrder(order *model.DemoOrder) (err error) {
	err = osi.orderDao.AddNewOrder(order)
	if err!=nil {
		return err
	}
	return nil
}

func (osi *OrderServiceImpl) GetOneOrder (orderNo string)(order *model.DemoOrder,err error)  {

	order, err = osi.orderDao.GetOneOrder(orderNo)
	if err !=nil{
		return nil, err
	}
	return order,nil
}

func (osi *OrderServiceImpl) UpdateOrder(order *model.DemoOrder) (err error){
	//获取旧对象
	//oldOrder, err := osi.orderDao.GetOneOrder(order.OrderNo)
	//if err!=nil {
	//	return err
	//}
	err = osi.orderDao.UpdateOrder(order)
	/*UpdateOrder := model.NewDemoOrder(orderNo, "张三", amount, status, fileUrl)*/
	//生存新对象
	//err = osi.orderDao.(&order)
	if err!=nil{
		return err
	}
	return nil
}

//todo 没看懂语法(osi *OrderServiceImpl)，不过知道这个要干啥
func (osi *OrderServiceImpl) DeleteOrder (orderNo string) (err error){
	//delOrder := model.NewDemoOrder(orderNo, "", 2.2, "", "")
	delOrder, err := osi.orderDao.GetOneOrder(orderNo)
	if err!=nil {
		return err
	}
	err = osi.orderDao.DeleteOrder(delOrder)
	if err!=nil {
		return err
	}
	return nil
}